<?php
// +---------------------------------------------------------------------+
// | OneBase    | [ WE CAN DO IT JUST THINK ]                            |
// +---------------------------------------------------------------------+
// | Licensed   | http://www.apache.org/licenses/LICENSE-2.0 )           |
// +---------------------------------------------------------------------+
// | Author     | 王勇程 <laughskydragon@qq.com>                         |
// +---------------------------------------------------------------------+
// | Repository | https://gitee.com/Bigotry/OneBase                      |
// +---------------------------------------------------------------------+

namespace app\admin\logic;

use app\common\logic\Withdraw as CommonWithdraw;

/**
 * 提现管理逻辑
 */
class Withdraw extends AdminBase
{
    
    public static $commonWithdrawLogic = null;
    
    /**
     * 基类初始化
     */
    public function __construct()
    {
        // 执行父类构造方法
        parent::__construct();
        
        empty(static::$commonWithdrawLogic) && static::$commonWithdrawLogic = new CommonWithdraw();
    }
    
    /**
     * 获取提现列表搜索条件
     * @param array $data 搜索参数
     * @return array
     */
    public function getWhere($data = [])
    {
        return static::$commonWithdrawLogic->getWhere($data);
    }
    
    /**
     * 获取提现列表
     */
    public function getWithdrawList($where = [], $field = true, $order = 'create_time desc', $paginate = DB_LIST_ROWS)
    {
        return $this->modelWithdrawOrder->getList($where, $field, $order, $paginate);
    }
    
    /**
     * 获取提现信息
     */
    public function getWithdrawInfo($where = [], $field = true)
    {
        return static::$commonWithdrawLogic->getWithdrawInfo($where, $field);
    }
    
    /**
     * 提现审核
     */
    public function withdrawReview($data = [])
    {
        $url = url('index');
        
        $validate_result = $this->validateWithdraw->scene('review')->check($data);
        
        if (!$validate_result) {
            return [RESULT_ERROR, $this->validateWithdraw->getError()];
        }
        
        $result = static::$commonWithdrawLogic->withdrawReview($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('审核', '提现审核，id：' . $data['id']);
        
        return [RESULT_SUCCESS, '审核成功', $url];
    }
    
    /**
     * 批量审核
     */
    public function batchReview($data = [])
    {
        $url = url('index');
        
        if (empty($data['ids']) || empty($data['withdraw_status'])) {
            return [RESULT_ERROR, '参数错误'];
        }
        
        $result = static::$commonWithdrawLogic->batchReview($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('批量审核', '批量审核提现，ids：' . implode(',', $data['ids']));
        
        return [RESULT_SUCCESS, '批量审核成功', $url];
    }
    
    /**
     * 获取提现统计
     */
    public function getWithdrawStats($data = [])
    {
        return static::$commonWithdrawLogic->getWithdrawStats($data);
    }
    
    /**
     * 导出提现列表
     */
    public function exportWithdrawList($where = [], $field = true, $order = 'create_time desc')
    {
        $list = static::$commonWithdrawLogic->getWithdrawList($where, $field, $order, false);
        
        $titles = "用户ID,提现金额,手续费,实际到账,状态,申请时间,审核时间";
        $keys   = "user_id,amount,fee,real_amount,status_text,create_time,review_time";
        
        action_log('导出', '导出提现列表');
        
        export_excel($titles, $keys, $list, '提现列表');
    }
    
    /**
     * 提现设置
     */
    public function withdrawSettings($data = [])
    {
        $url = url('settings');
        
        $result = static::$commonWithdrawLogic->withdrawSettings($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('设置', '提现设置更新');
        
        return [RESULT_SUCCESS, '设置保存成功', $url];
    }
    
    /**
     * 获取提现设置
     */
    public function getWithdrawSettings()
    {
        return static::$commonWithdrawLogic->getWithdrawSettings();
    }
    
    /**
     * 获取提现统计（控制器调用的方法名）
     */
    public function getWithdrawStatistics($data = [])
    {
        return static::$commonWithdrawLogic->getWithdrawStatistics($data);
    }
    
    /**
     * 获取提现配置（控制器调用的方法名）
     */
    public function getWithdrawConfig($data = [])
    {
        return static::$commonWithdrawLogic->getWithdrawConfig($data);
    }
    
    /**
     * 保存提现配置（控制器调用的方法名）
     */
    public function saveWithdrawConfig($data = [])
    {
        $url = url('withdrawConfig');
        
        $result = static::$commonWithdrawLogic->saveWithdrawConfig($data);
        
        if (is_array($result) && $result[0] == RESULT_ERROR) {
            return $result;
        }
        
        action_log('设置', '提现配置更新');
        
        return [RESULT_SUCCESS, '配置保存成功', $url];
    }
}